package ar.fiuba.logger.filter;

import ar.fiuba.logger.LogFilter;
import ar.fiuba.logger.LogLevel;
import ar.fiuba.logger.LogMessageInterface;

/**
 * The Class LogLevelLessThanOrEqualToThresholdFilter.
 * 
 * This class implements a filter that won't allow messages with log level greater than a threshold. 
 */
public class LogLevelLessThanOrEqualToThresholdFilter implements LogFilter {

  /** The log level threshold. */
  private LogLevel threshold;

  /**
   * Instantiates a new log level less than or equal to threshold filter.
   *
   * @param threshold the threshold
   */
  public LogLevelLessThanOrEqualToThresholdFilter(LogLevel threshold) {
    this.threshold = threshold;
  }

  /* (non-Javadoc)
   * @see ar.fiuba.logger.filter.LogFilter#allow(ar.fiuba.logger.LogMessageInterface)
   */
  @Override
  public Boolean allow(LogMessageInterface logMessage) {
    return logMessage.getLogLevel().isEqualOrWorseThan(threshold);
  }

  /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    LogLevelLessThanOrEqualToThresholdFilter other = (LogLevelLessThanOrEqualToThresholdFilter) obj;
    if (threshold != other.threshold)
      return false;
    return true;
  }

  /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((threshold == null) ? 0 : threshold.hashCode());
    return result;
  }

}
